// import {asyncPost} from '~/assets/js/api'
import { Toast } from 'vant'

let cantClick = false
const notifyUrl = 'http://ysrest.yaleai.com/api/platform/pay/notify'

export default function pay(amount, orderNo, userId, openid) {
  let json = {
    userId, // 被支持者id
    payPlatform: 2, // 支付方式 2 微信支付
    productOrderNo: orderNo,
  }
  if (cantClick) return
  cantClick = true
  return new Promise((resolve, reject) => {
    global.FUQIANLA.init({
      app_id: 'PU67NtmbXO0tio0N9ungvA',
      order_no: Date.now(),
      amount: String(Math.floor((Number(amount) * 100))),
      channel: 'wx_pay_pub',
      subject: '支付说明', // 支付说明
      notify_url: notifyUrl,
      optional: JSON.stringify(json),
      extra: {
        openid,
        cb: data => {
          cantClick = false
          Toast.success('支付成功')
          resolve(data)
        },
        ecb: data => {
          cantClick = false
          Toast.success('支付失败');
          reject(data)
        }
      }
    })
  })
}